package com.hotel.search.service.impl;

import com.hotel.comm.bean.Constants;
import com.hotel.comm.bean.DubboReply;
import com.hotel.comm.bean.EnumConstants.CompanyScore;
import com.hotel.comm.bean.EnumConstants.SalaryRange;
import com.hotel.comm.bean.IReply;
import com.hotel.comm.bean.PageConfig;
import com.hotel.comm.bean.lucene.IndexableBean;
import com.hotel.comm.bean.lucene.PositionIndexBean;
import com.hotel.comm.lucene.condition.ICondition;
import com.hotel.comm.lucene.condition.SearchCondition;
import com.hotel.comm.lucene.condition.SearchSort;
import com.hotel.comm.lucene.function.AndSearchCondition;
import com.hotel.comm.lucene.function.OrSearchCondition;
import com.hotel.comm.service.BaseSearchServiceImpl;
import com.hotel.comm.service.search.ILuceneSearchService;
import com.hotel.search.lucene.LuceneSearch;
import com.hotel.search.lucene.LuceneSearchCondition;
import com.hotel.search.lucene.LuceneSearchFactory;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class LuceneSearchServiceImpl extends BaseSearchServiceImpl implements ILuceneSearchService {
    private static final int SALARY_LOW = 0;
    private static final int SALARY_HIGHT = 99999999;

//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	public IReply searchByCityId(String cityId, int currentPage, int pageSize) {
//		LuceneSearch search = null;
//		try {
//			if(StringUtils.isBlank(cityId)){
//				return new DubboReply(false);
//			}
//			search = LuceneSearchFactory.getPositionSearchFactory().getSearch();
//			SearchCondition searchCondition = new SearchCondition("cityId", Long.parseLong(cityId));
//			SearchSort[] searchSorts = this.getDefaultPositionSorts();
//			LuceneSearchCondition condition = null;
//			if(pageSize <= 0){
//				condition = new LuceneSearchCondition(currentPage, searchSorts, searchCondition);
//			}else {
//				condition = new LuceneSearchCondition(currentPage, pageSize, searchSorts, searchCondition);
//			}
//			PageConfig<IndexableBean> pageConfig = search.search(condition, PositionIndexBean.class);
//			logger.info("Position Search: " + pageConfig);
//			return new DubboReply(true, pageConfig);
//		} catch (Exception e) {
//			e.printStackTrace();
//			return null;
//		}finally{
//			if(search != null){
//				LuceneSearchFactory.getPositionSearchFactory().releaseSearch(search);
//			}
//		}
//	}
//
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	public IReply searchByPositionNameAndCityId(String positionName, String cityId, int currentPage, int pageSize) {
//		LuceneSearch search = null;
//		try {
//			if(StringUtils.isBlank(positionName)){
//				return new DubboReply(false);
//			}
//			if(StringUtils.isBlank(cityId)){
//				return new DubboReply(false);
//			}
//			search = LuceneSearchFactory.getPositionSearchFactory().getSearch();
//			List<SearchCondition> conditions = new ArrayList<SearchCondition>();
//			conditions.add(new SearchCondition("cityId", Long.parseLong(cityId)));
//			conditions.add(new SearchCondition("positionName", positionName));
//			SearchSort[] searchSorts = this.getDefaultPositionSorts();
//			LuceneSearchCondition condition = null;
//			if(pageSize <= 0){
//				condition = new LuceneSearchCondition(currentPage, searchSorts, new AndSearchCondition(conditions));
//			}else {
//				condition = new LuceneSearchCondition(currentPage, pageSize, searchSorts, new AndSearchCondition(conditions));
//			}
//			PageConfig<IndexableBean> positions = search.search(condition, EjobCompanyPosition.class);
//			return new DubboReply(true, positions);
//		} catch (Exception e) {
//			return null;
//		}finally{
//			if(search != null){
//				LuceneSearchFactory.getPositionSearchFactory().releaseSearch(search);
//			}
//		}
//	}
//
//	@SuppressWarnings({ "unchecked", "rawtypes" })
//	public IReply searchByCompanyNameAndCityId(String companyName, String cityId, int currentPage, int pageSize) {
//		LuceneSearch search = null;
//		try {
//			if(StringUtils.isBlank(companyName)){
//				return new DubboReply(false);
//			}
//			if(StringUtils.isBlank(cityId)){
//				return new DubboReply(false);
//			}
//			search = LuceneSearchFactory.getPositionSearchFactory().getSearch();
//			List<SearchCondition> conditions = new ArrayList<SearchCondition>();
//			conditions.add(new SearchCondition("cityId", Long.parseLong(cityId)));
//			conditions.add(new SearchCondition("companyName", companyName));
//			SearchSort[] searchSorts = this.getDefaultPositionSorts();
//			LuceneSearchCondition condition = null;
//			if(pageSize <= 0){
//				condition = new LuceneSearchCondition(currentPage, searchSorts, new AndSearchCondition(conditions));
//			}else {
//				condition = new LuceneSearchCondition(currentPage, pageSize, searchSorts, new AndSearchCondition(conditions));
//			}
//			PageConfig<IndexableBean> positions = search.search(condition, EjobCompanyPosition.class);
//			return new DubboReply(true, positions);
//		} catch (Exception e) {
//			return null;
//		}finally{
//			if(search != null){
//				LuceneSearchFactory.getPositionSearchFactory().releaseSearch(search);
//			}
//		}
//	}

    @SuppressWarnings({"unchecked", "rawtypes"})
    @Override
    public IReply searchPosition(String cityId, String positionName, String positionType, String workingLife,
                                 String salaryRange, String companyName, String companyNature, String companySize, String companyScore,
                                 String educationRequirements, int currentPage, int pageSize) {
        LuceneSearch search = null;
        try {
            search = LuceneSearchFactory.getPositionSearchFactory().getSearch();
            List<ICondition> conditions = new ArrayList<ICondition>();
            if (StringUtils.isNotBlank(cityId)) {
                conditions.add(new SearchCondition("cityId", cityId.trim()));
            }
            if (StringUtils.isNotBlank(positionName)) {
                conditions.add(new SearchCondition("positionName", positionName.trim()));
            }
            if (StringUtils.isNotBlank(positionType)) {
                conditions.add(new SearchCondition("positionType", positionType.trim()));
            }
            if (StringUtils.isNotBlank(workingLife)) {
                conditions.add(new SearchCondition("workingLife", workingLife.trim()));
            }
            if (StringUtils.isNotBlank(companyName)) {
                conditions.add(new SearchCondition("companyName", companyName.trim()));
            }
            if (StringUtils.isNotBlank(companyNature)) {
                conditions.add(new SearchCondition("companyNature", companyNature.trim()));
            }
            if (StringUtils.isNotBlank(salaryRange)) {
                ICondition _salaryRange = this.getSalaryCondition(salaryRange.trim());
                conditions.add(_salaryRange);
            }
            if (StringUtils.isNotBlank(companySize)) {
                conditions.add(new SearchCondition("companySize", companySize.trim()));
            }
            if (StringUtils.isNotBlank(companyScore)) {
                CompanyScore _companyScore = CompanyScore.valueOf(companyScore);
                conditions.add(new SearchCondition("companyScore", _companyScore.getScoreLow(), _companyScore.getScoreHight()));
            }
            if (StringUtils.isNotBlank(educationRequirements)) {
                conditions.add(new SearchCondition("educationRequirements", educationRequirements.trim()));
            }
            if (CollectionUtils.isEmpty(conditions)) {
                return new DubboReply(Constants.REPLY_CODE_PARAMCHECKFAILED, Constants.REPLY_MSG_PARAMCHECKFAILED);
            }
            SearchSort[] searchSorts = this.getDefaultPositionSorts();
            LuceneSearchCondition condition = null;
            if (pageSize <= 0) {
                condition = new LuceneSearchCondition(currentPage, searchSorts, new AndSearchCondition(conditions));
            } else {
                condition = new LuceneSearchCondition(currentPage, pageSize, searchSorts, new AndSearchCondition(conditions));
            }
            PageConfig<IndexableBean> positions = search.search(condition, PositionIndexBean.class);
            return new DubboReply(true, positions);
        } catch (Exception e) {
            e.printStackTrace();
            return new DubboReply(false);
        } finally {
            if (search != null) {
                LuceneSearchFactory.getPositionSearchFactory().releaseSearch(search);
            }
        }
    }

    private ICondition getSalaryCondition(String salaryRange) {
        SalaryRange _salaryRange = SalaryRange.valueOf(salaryRange);
        Integer salaryLow = _salaryRange.getSalaryLow();
        Integer salaryHight = _salaryRange.getSalaryHight();
        //搜索(<10000)
        if (salaryLow == null) {
            return new SearchCondition("salaryLow", SALARY_LOW, salaryHight);
        }
        //搜索(>5000)
        if (salaryHight == null) {
            return new SearchCondition("salaryHight", salaryLow, SALARY_HIGHT);
        }
        //搜索(5000-10000)   库中值2000-6000 2000-12000
        List<ICondition> andConditions = new ArrayList<ICondition>();
        List<ICondition> orConditions = new ArrayList<ICondition>();
        andConditions.add(new SearchCondition("salaryLow", SALARY_LOW, salaryLow));
        andConditions.add(new SearchCondition("salaryHight", salaryLow, SALARY_HIGHT));
        ICondition one = new AndSearchCondition(andConditions);
        orConditions.add(one);

        //搜索(5000-10000)   库中值7000-12000
        andConditions = new ArrayList<ICondition>();
        andConditions.add(new SearchCondition("salaryLow", salaryLow, salaryHight));
        andConditions.add(new SearchCondition("salaryHight", salaryHight, SALARY_HIGHT));
        ICondition tow = new AndSearchCondition(andConditions);
        orConditions.add(tow);

        return new OrSearchCondition(orConditions);
    }

    private SearchCondition getCompanyScoreCondition(String companyScoreLow, String companyScoreHight) {
        Integer _companyScoreLow = null, _companyScoreHight = null;
        if (StringUtils.isNotBlank(companyScoreLow)) {
            _companyScoreLow = Integer.parseInt(companyScoreLow);
        }
        if (StringUtils.isNotBlank(companyScoreHight)) {
            _companyScoreHight = Integer.parseInt(companyScoreHight);
        }
        if (_companyScoreLow == null) {
            return new SearchCondition("companyScore", SALARY_LOW, _companyScoreHight);
        }
        if (_companyScoreHight == null) {
            return new SearchCondition("companyScore", _companyScoreLow, SALARY_HIGHT);
        }
        return new SearchCondition("companyScore", _companyScoreLow, _companyScoreHight);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public IReply clearPosition() {
        LuceneSearchFactory.getPositionSearchFactory().clearPosition();
        return new DubboReply();
    }

}
